<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>导出Excel</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap-select.css" rel="stylesheet">
<link href="../css/font-awesome.min.css" rel="stylesheet">
<link type="text/css" href="../css/RxWindow.css" rel="Stylesheet" />
<link href="../css/JyyCommon.css" rel="stylesheet">
<link href="../css/JyyTable.css" rel="stylesheet">
<link href="../css/JyyTree.css" rel="stylesheet">
<link href="../css/JyyMsgBox.css" rel="stylesheet">
<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="../js/jquery.form.min.js"></script> 
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-select.js"></script>
<script type="text/javascript" src="../js/JyyCommon.js"></script>
<script type="text/javascript" src="../js/RxWindow.js" ></script>
<script type="text/javascript" src="../js/JyyTable.js"></script>
<script type="text/javascript" src="../js/JyyTree.js"></script>
<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
<script type="text/javascript" src="../js/JyyLoading.js"></script>
<style type="text/css">
.heidenbt{
	display: none
}
body {
	background: #ffffff;
}
</style>
<script type="text/javascript">
var table = null;
function getQueryString (name){
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	return r ? unescape(r[2]) : null;
}
var treeCode =  this.getQueryString("code");
var planId =  this.getQueryString("projectId")||'88888888888';
$(function(){
	$.when(getTableData(treeCode)).done(function(tableData){
		if(table == null){
			table = JyyTable.init("table", tableData, {position:"absolute",top:"50px",left:"1px",right:"5px",bottom:"0px",overflow:"hidden"}, onTableEvent);
			table.hideCols(true);
		}else{
			table.refresh(tableData);
			table.hideCols(true);
		}
	});

	$('#btnCancel').click(function(){
		$('#wizardMask').hide();
		$('#jiamiPanel').hide();
	});
	$('#btnFinish').click(function(){
		$('#wizardMask').hide();
		$('#jiamiPanel').hide();
		download($("#inPassWord").val());
	});
});

function getTableData(code){
	var defer = $.Deferred();
	var result = null;
	var data = {"projectid":planId,"code":code};
	
	$.ajax({
		dataType: "json",
		async:true,
		data:data,
		url: "../pv/page/pageInfo/meosExportExcel"
	}).done(function(res) {
		var sts=eval("("+res+")");
		result = sts;
		defer.resolve(sts);
	});
	return defer.promise();
}

//下载按钮
function download(ifEncry){
	var checkTable = table.getCheckValue();
	if(checkTable.length == 1) {
		JyyMsgBox.alert({ title:"警告", message:"请选择记录",type:"warning"}).on(function (e) {});
		return false;
	}
	var indexcodes = "";
	for(var i = 1;i < checkTable.length;i++) {
		indexcodes += checkTable[i][2];
		if(i < checkTable.length-1) indexcodes += ",";
	}
	var form = $('<form action="../pv/pageExcel/downloadExcel/'+ treeCode +'" style="display:none" method="post">');
	var input = $('<input type="hidden" name="projectid">');
	input.attr("value",planId);
	form.append(input);
	var input1 = $('<input type="hidden" name="indexcodes">');
	input1.attr("value",indexcodes);
	form.append(input1);
	if(ifEncry == 1){
		var input2 = $('<input type="hidden" name="ifEncry">');
		input2.attr("value",ifEncry);
		form.append(input2);
	}
	$("body").append(form);
	form.submit();
	form.remove();
}

function onTableEvent(type, value){
	console.log("onTableEvent");
	if(type == "insertBefore"){
		console.log("insertBefore");
		//核查数据正确性
		for(var a = 0 ; a < value[1].length; a++){
			if(table.config.hideCols.indexOf(a) <= -1){
				if(JyyCommon.isEmpty(value[1][a])){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
					return false;
				}else if((!JyyCommon.isObject(value[1][a]) && value[1][a].indexOf("请选择")>-1) || value[1][a] == " "){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]下拉框未做选择",type:"warning"}).on(function (e) {});
					return false;
				}else if(JyyCommon.isObject(value[1][a]) && JyyCommon.isEmpty(value[1][a].name)){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
					return false;
				}
			}
		}
		return true;
	} else if(type == "data"){
		JyyLoading.start();
			 $.ajax({
            type:"POST",
            url:"../pv/page/check/"+treeCode,
            data:{"value":JSON.stringify(value),"projectid":planId},
            success:function(data){
            	var pageSize = table.config.page.option.pageSize;
				$.when(getTableData(treeCode)).done(function(tableData){
					table.refresh(tableData);
					JyyLoading.end();
				});
            }
        }); 
	} else if(type == "page"){
		JyyLoading.start();
		$.when(getTableData(treeCode)).done(function(tableData){
			table.refresh(tableData);
			JyyLoading.end();
		});
	}
}
</script>
</head>
<body>
	<div id="operate" style="height: 50px;">
		<div class="col-md-12" style="position: absolute;top: 10px;padding-left: 5px;padding-right: 5px;">
			<button id="daochu" class="btn btn-info btn-sm" onclick=download(0)><i class="glyphicon glyphicon-download"></i>excel导出</button>
			<button id="jiami" class="btn btn-info btn-sm" onclick=download(1)><i class="glyphicon glyphicon-download"></i>加密导出</button>
		</div>
	</div>
	<div id="table"></div>
</body>
</html>